#include "UtilityPCH.h"
#include "UT_BxDFHelper.h"
#include "UT_MathHelper.h"
#include "UT_TAlgorithm.h"

using namespace Utility;

//---------------------------------------------------------------------------
//UT_FresnelConductor
//---------------------------------------------------------------------------
UT_FresnelConductor::UT_FresnelConductor(const UT_SpectrumUsed &rkEta, const UT_SpectrumUsed& rkK)
	: m_kEta(rkEta)
	, m_kK(rkK)
{

}
//---------------------------------------------------------------------------
UT_SpectrumUsed UT_FresnelConductor::Evaluate(float fCosi) const
{
	return UT_MathHelper::ComputeFresnelReflectionWithConductor(UT_MathHelper::Abs(fCosi),m_kEta,m_kK);
}
//---------------------------------------------------------------------------
//UT_F
//---------------------------------------------------------------------------
UT_FresnelDielectric::UT_FresnelDielectric(float fEtai, float fEtat)
	: m_fEtai(fEtai)
	, m_fEtat(fEtat)
{

}
//---------------------------------------------------------------------------
UT_SpectrumUsed UT_FresnelDielectric::Evaluate(float fCosi) const
{
	// Compute Fresnel reflectance for dielectric
	fCosi = UT_MathHelper::Clamp(fCosi, -1.f, 1.f);

	// Compute indices of refraction for dielectric
	bool bEntering = fCosi > 0.;
	float ei = m_fEtai, et = m_fEtat;
	if (!bEntering)
		ut_swap(ei, et);

	// Compute _sint_ using Snell's law
	float fSint = ei/et * UT_MathHelper::Sqrt(UT_MathHelper::Max(0.f, 1.f - fCosi*fCosi));
	if (fSint >= 1.) 
	{
		// Handle total internal reflection
		return 1.;
	}
	else
	{
		float fCost = UT_MathHelper::Sqrt(UT_MathHelper::Max(0.f, 1.f - fSint*fSint));
		return UT_MathHelper::ComputeFresnelReflectionWithDielectirc(UT_MathHelper::Abs(fCosi),fCost,m_fEtai,m_fEtat);
	}
}
//---------------------------------------------------------------------------